<?php
/* +----------------------------------------------------------------------
 * 作          者：huk
 +----------------------------------------------------------------------
 * 创建日期：2016-08-27
 +----------------------------------------------------------------------
 * 文件描述：测验内容>>附件题
 +----------------------------------------------------------------------
 * 升级记录： 
 +----------------------------------------------------------------------
*/
class AffixAction extends CommonAction {
	
	public function get_item_edit($formid) {
		//查询数据
			$model = new AffixModel();
			$item = new QuestionTestSelectItemModel();
			$data = $model->field("id,
								   title,
								   code,
								   score,
								   ext,
								   size,
								   operator,
								   update_date")
						  ->where("id = '".$formid."'")
						  ->find();
		/*//查询当前选择题选项
		$data['item_list'] = $item->field("id,
										   intro,
										   is_correct,
										   seq,
										   score")
								  ->where("selectid = '".$data['id']."'")
								  ->order("seq ASC")
								  ->select();*/
	    
		return $data;
	}
	
	
	public function get_item_detail($roomid,$item_id, $userid) {
		//查询数据
		if(!empty($roomid)) {
			$model = new RoomCourseTestSelectModel();
			$item = new RoomCourseTestSelectItemModel();
			$answer = new RoomCourseTestSelectAnswerModel();
			$where['oldid'] = $item_id;
			$where['roomid'] = $roomid;
		} else{
			$model = new CourseTestSelectModel();
			$item = new CourseTestSelectItemModel();
			$answer = new CourseTestSelectAnswerModel();
			$where['id'] = $item_id;
		}
		$data = $model->field("id,
							   testid,
							   question,
							   answer,
							   score,
							   type,
							   get_field_dict_name('course_test_select', 'type', type) type_name")
					  ->where($where)
					  ->find();
		
		//查询当前选择题选项
		$data['item_list'] = $item->field("id,
										   intro,
										   is_correct,
										   seq")
								  ->where("selectid = '".$data['id']."'")
								  ->order("seq ASC")
								  ->select();
		
		//查询当前用户作答情况
		$data['user_answer'] = $answer->field("answer,
											   score")
									  ->where("selectid = '".$data['id']."'
									  	   and operator = '".$userid."'")
									  ->find();
		return $data;
	}

	/**
	 * 题目添加页面
	 */
	public function add() {
		//当前所剩分值
		
		$roomid = $this->_request('formid');
		
		$this->assign('item_info', $this->get_form_info($this->_request('formid')));	//课程基本信息（头部显示）
		$this->assign('type_list', set_field_dict_list('affix', 'ext'));
		$this->display();
	}
	
	/**
	 * 将题目添加至数据库
	 */
	public function insert() {
		$post_data = $this->_request();
		$post_data_ext = $this->_request('ext');
		$ext = implode(',',$post_data_ext);
		$adddata['code'] = $post_data['code'];
		$adddata['title'] = $post_data['title'];
		$adddata['intro'] = $post_data['intro'];
		$adddata['ext'] = $ext;
		$adddata['score'] = $post_data['score'];
		$adddata['size'] = $post_data['size'];
		$formid = $this->_request('formid');
		$mode = $this->_request('mode');
		$affixmodel = new AffixModel();
		$dataaffix = $affixmodel->create($adddata,1);
		if(false !== $dataaffix) {
			$affixdata = $affixmodel->add();
			$data = R('Admin/QuestionTestEdit/add_item_info', array($formid, 'affix', $affixdata));
			if(false !== $data) {
				$this->success('附件题题添加成功！',__APP__.'?g=Admin&m=QuestionTestEdit&a=index&formid='.$formid.'&mode='.$mode.'#affix_'.$affixdata);
			} else {
		  		$this->error('附件题添加失败');
			}
		} else {
			$this->error('添加失败'.$affixmodel->getError());
		}
		
	}
	
	/**
	 * 题目修改页面
	 */
	public function edit() {
		//查询当前选择题信息
		$roomid = $this->_request('roomid');
		$model = new AffixModel();
		$data = $model->where("id = '".$_REQUEST['questionid']."'")->find();
		$this->assign('data', $data);
		
		/*//查询当前选项信息
		
		$item_list = $item->where("selectid = '".$data['id']."'")->order("seq ASC")->select();
		$this->assign('item_list', $item_list);
		
		//当前所剩分值
		$current_test_score = R('Course/CourseTestEdit/current_test_score', array($_REQUEST['testid']));
		$this->assign('can_use_score', (100 - (int)$current_test_score + (int)$data['score']));
		
		$this->assign('course_info', $this->get_course_info($_REQUEST['courseid']));	//课程基本信息（头部显示）
		$this->assign('type_list', set_field_dict_list('course_test_select', 'type'));*/
		$this->display();
	}
	
    /**
	 * 将题目信息更新至数据库
	 */
	public function update() {
		$post_data = $this->_request();
		$post_data_ext = $this->_request('ext');
		$ext = implode(',',$post_data_ext);
		$adddata['code'] = $post_data['code'];
		$adddata['title'] = $post_data['title'];
		$adddata['intro'] = $post_data['intro'];
		$adddata['score'] = $post_data['score'];
		$adddata['ext'] = $ext;
		$adddata['size'] = $post_data['size'];
		$questionid = $this->_request('questionid');
		$formid = $this->_request('formid');
		$mode = $this->_request('mode');
		$AffixModel = new AffixModel();
		$AffixModel->startTrans();//启动事务
		if(false !== $AffixModel->create($adddata,1)) {
			if(false !== $AffixModel->where("id = '".$questionid."'")->save($adddata)) {
			
				$AffixModel->commit();//提交事务
				$this->success('题目更新成功！',__APP__.'?g=Admin&m=QuestionTestEdit&a=index&formid='.$formid.'&mode='.$mode.'#affix_'.$questionid);
					
			} else {
				$AffixModel->rollback();//回滚事务
				$this->error('题目更新失败！!!!!!');
			}
		} else {
			$AffixModel->rollback();//回滚事务
			$this->error('题目更新失败！'.$AffixModel->getError());
		}
	}

	
    /**
	 * 删除题目信息
	 */
	public function delete() {
		$id = $_REQUEST['formid'];
		$affixmodel = new AffixModel();
		$model = new QuestionTestSelectModel();	
			$model->startTrans(); //启动事务
				//删除选择题基本信息
				if(false !== $affixmodel->where("id = '".$id."'")->delete()) {
					//删除测验中的item_info字段
						$model->commit();	//提交事务
						$this->ajaxReturn(null, '删除成功！', 1);
					} else {
						$model->rollback();	//回滚事务
						$this->ajaxReturn(null, '删除失败！', 0);
					}
				 
			}
		
	
	
	/**
	 * 编辑选择题时从数据库删除选项操作
	 */
	public function delItem() {
		$roomid = $this->_request('roomid');
		if(!empty($roomid)) {
			$model = new RoomCourseTestSelectItemModel();
		} else{
			$model = new CourseTestSelectItemModel();
		}
		if(false !== $model->where("id = '".$_REQUEST['id']."'")->delete()) {
			$this->ajaxReturn(null, '选项从数据库中删除成功！', 1);
		} else {
			$this->ajaxReturn(null, '选项从数据库中删除失败！', 0);
		}
	}
}
?>